package cn.com.dhcc.app.pub.core.listener;

import java.io.IOException;

import javax.servlet.ServletContextEvent;

import cn.com.dhcc.app.core.AppEnv.ParaGetTool;
import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ISocketHandler;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ISocketServer;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;

/**
 * 启动同步用的socket服务成功
 */
public class SocketInitListener implements AppInitListener {

	ISocketServer iSocketServer = null;
	int  appSyncServerPort = ParaGetTool.getPara(Integer.class, "app.syncserver.port", 9160);
	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam) throws InitException {
		try {
			iSocketServer = (ISocketServer) CM.getBeanByName(IocBean.MINA_SOCKET_SERVER);
			ISocketHandler socketHandler = (ISocketHandler) CM.getBeanByName(AppIocBean.TURBO_SOCKETIO_HANDLER);
			iSocketServer.start(appSyncServerPort,socketHandler);
			contextEvent.getServletContext().log("启动同步用mina socket 服务成功,端口["+appSyncServerPort+"]");
		} catch (IOException e) {
			contextEvent.getServletContext().log("启动同步用mina socket 服务失败",e);
		}
	}
	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam) throws Exception {
		try {
			iSocketServer.stop();
			contextEvent.getServletContext().log("关闭mina socket server 成功.");
		} catch (Exception e) {
			throw e;
		}
	}
}
